package com.useeinfo.oa.modules.wuHanCommon.base.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.oa.modules.wuHanCommon.base.entity.WarningGroup;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("WarningGroupDao")
public class WarningGroupDao implements CrudDao<WarningGroup> {
    private final static Logger logger = LoggerFactory.getLogger(WarningGroupDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();

        conditionHash.put("deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        String name = queryHash.get("name");
        if (StringUtils.isNotBlank(name)) {
            conditionHash.put("name = ?{paramIndex} ", name);
        }
        String code = queryHash.get("code");
        if (StringUtils.isNotBlank(code)) {
            conditionHash.put("code = ?{paramIndex} ", code);
        }
        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(w) from WarningGroup w ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<WarningGroup> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select w from WarningGroup w ", "order by w.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<WarningGroup> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, WarningGroup.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public WarningGroup findModel(Long id) {
        return entityManager.find(WarningGroup.class, id);
    }

    @Override
    public Integer add(WarningGroup model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("WarningGroupDaoImpl添加WarningGroup成功！");
        return 1;
    }

    @Override
    public Integer update(WarningGroup model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        WarningGroup warningGroup = entityManager.find(WarningGroup.class, id);
        entityManager.remove(warningGroup);
        return 1;
    }

    @Override
    public Integer deleteLogic(WarningGroup model) {
        WarningGroup warningGroup = entityManager.find(WarningGroup.class, model.getId());
        warningGroup.setDeleteStatus(true);
        return 1;
    }
}
